import 'package:async/async.dart';
import 'package:boss/provider/regist_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/pickerAddress/picker_address_page.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:boss/ui/widget/title_row_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'store_type_item.dart';

class CompleteMessageBrowser extends StatefulWidget {
  @override
  _CompleteMessageBrowserState createState() => _CompleteMessageBrowserState();
}

class _CompleteMessageBrowserState extends State<CompleteMessageBrowser> {
  BuildContext context;

  Future<Result> _loadTask() async {
    var registProvider = Provider.of<RegistProvider>(context, listen: false);
    return registProvider.initStoreType();
  }

  @override
  Widget build(BuildContext context) {
    this.context = context;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.zero,
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 30.w,
                  ),
                  child: Consumer<RegistProvider>(
                    builder: (context, registProvider, child) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          BaseTextFormField(
                            hintText: '输入您的餐厅名称',
                            textAlign: TextAlign.end,
                            initialValue: registProvider.storeName,
                            onChanged: (value) {
                              registProvider.setStoreName(value);
                            },
                          ),
                          TitleRowButton(
                            title: '所在地区',
                            hintText: '请选择',
                            value: registProvider.addressText,
                            onTap: pickerPca,
                          ),
                          Container(
                            margin: EdgeInsets.symmetric(
                              vertical: ScreenUtil.instance.setHeight(27.5),
                            ),
                            child: Text(
                              '请选择您的餐厅类型',
                              style: TextStyleConstant().normal_1_28,
                            ),
                          ),
                        ],
                      );
                    },
                  ),
                ),
                Consumer<RegistProvider>(
                  builder: (context, registProvider, child) {
                    return Loader(
                      loadTask: _loadTask,
                      builder: (context) {
                        return GridView.builder(
                          padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 30.w),
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3,
                            childAspectRatio: 21 / 14,
                            mainAxisSpacing: 30.w,
                            crossAxisSpacing: 30.w,
                          ),
                          itemBuilder: (context, index) {
                            var current = registProvider.storeTypes[index];
                            var isSelected = registProvider.selectedStoreType?.storeTypeId == current.storeTypeId;

                            return StoreTypeItem(
                              data: current,
                              isSelected: isSelected,
                              onTap: () {
                                registProvider.setStoreType(current);
                              },
                            );
                          },
                          itemCount: registProvider.storeTypes.length,
                        );
                      },
                    );
                  },
                ),
              ],
            ),
          ),
        ),
        Container(
          height: 118.w + MediaQuery.of(context).padding.bottom,
          padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
          decoration: BoxDecoration(color: Colors.white, boxShadow: [DEFAULT_SHADOW]),
          alignment: Alignment.center,
          child: RadiusButton(
            title: '进入首页',
            width: 590.w,
            onTap: () {
              var registProvider = Provider.of<RegistProvider>(context, listen: false);
              registProvider.registStore(context);
            },
          ),
        ),
      ],
    );
  }

  void pickerPca() async {
    var registProvider = Provider.of<RegistProvider>(
      context,
      listen: false,
    );

    final res = await showPickerAddressPage(context,
        data: registProvider.longitude == null
            ? null
            : PCDHData(
                provinces: registProvider.provinces,
                provincesCode: registProvider.provincesCode,
                city: registProvider.city,
                cityCode: registProvider.cityCode,
                district: registProvider.district,
                districtCode: registProvider.districtCode,
                address: registProvider.address,
                houseNumber: registProvider.houseNumber,
                latitude: registProvider.latitude,
                longitude: registProvider.longitude,
              ));

    if (res != null) {
      registProvider.setStoreAddress(res);
    }
  }
}
